package com.business.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.business.system.feign.GoodsShopFeign;
import com.business.system.mapper.ShopMapper;
import com.business.system.service.IShopService;
import com.business.system.vo.*;
import com.common.entity.Goods;
import com.common.entity.GoodsShop;
import com.common.entity.Shop;
import com.common.untils.CommonResult;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author pgx
 * @since 2022-08-31
 */
@Service
public class ShopServiceImpl implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private GoodsShopFeign goodsShopFeign;
    @Override
    public CommonResult findAllShop(ShopVo shopVo) {
        Page<Shop> page = new Page<>(shopVo.getCurrentPage(),shopVo.getPageSize());
        QueryWrapper<Shop> queryWrapper = new QueryWrapper<>();
        if(StringUtils.hasText(shopVo.getName())){
            queryWrapper.like("name",shopVo.getName());
        }
        if(StringUtils.hasText(shopVo.getAddress())){
            queryWrapper.like("address",shopVo.getAddress());
        }
        queryWrapper.eq("is_deleted",0);
        queryWrapper.eq("auditstart",1);
        queryWrapper.eq("auditresult",1);
        Page<Shop> shopPage = shopMapper.selectPage(page, queryWrapper);
        return new CommonResult(2000,"查询成功",shopPage);
    }

    @Override
    public CommonResult detailedMsg(Integer id) {
        List<Map> list = shopMapper.detailedMsg(id);
        return new CommonResult(2000,"查询成功",list);
    }

    @Override
    public CommonResult editStart(Integer start, Integer id) {
        if(start==0){
            Integer row = shopMapper.editStart(id);
            Integer row02 = goodsShopFeign.editGoodsState(id);
            return new CommonResult(2000,"修改成功",null);
        }else {
            Integer row = shopMapper.editStart02(id);
            Integer row02 = goodsShopFeign.editGoodsState02(id);
            return new CommonResult(2000,"修改成功",null);
        }
    }

    @Override
    public CommonResult findByIdShopMsg(Integer id) {
        Map map = shopMapper.findByIdShopMsg(id);
       return new CommonResult(2000,"查询成功",map);
    }

    @Override
    public IPage<Map> shopAndGoods(Integer currentPage,Integer pageSize, HomeGoodsType homeGoodsType) {
        Page<Goods> page = new Page(currentPage,pageSize);
        QueryWrapper<Goods> queryWrapper = new QueryWrapper();
        if(homeGoodsType.getOneSubjectId()!=null ){
            queryWrapper.eq("g.type_id", homeGoodsType.getOneSubjectId());
        }
        queryWrapper.eq("g.is_deleted",0);
        queryWrapper.eq("gs.state",0);
        queryWrapper.gt("gs.count",0);
        IPage<Map > iPage = shopMapper.shopAndGoods(page,queryWrapper);
        return iPage;
    }

    @Override
    public CommonResult selectShopMsgByBid(String homeToken) {
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get(homeToken);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject client = (JSONObject) map.get("client");
        Integer bid = client.getInteger("bid");
        List<Map> list = shopMapper.selectShopMsg(bid);
        return new CommonResult(2000,"查询成功",list);
    }

    @Override
    public CommonResult updateShopMsgByBid(String homeToken, ShopAndBussinessVo shopAndBussinessVo) {
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get(homeToken);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject client = (JSONObject) map.get("client");
        Integer bid = client.getInteger("bid");
        String username = client.getString("username");
        Integer tid = shopAndBussinessVo.getTid();
        Integer start = shopAndBussinessVo.getStart();
        String address = shopAndBussinessVo.getAddress();
        Integer row =  shopMapper.updateBusinessCid(bid,tid);
        Integer row01 = shopMapper.updateShopMsg(username,start,address);
        return new CommonResult(2000,"修改成功",null);
    }

    @Override
    public CommonResult findGoodsBySid(String homeToken, GoodsVo goodsVo) {
        System.out.println(goodsVo.getCurrentPage());
        System.out.println(goodsVo.getPageSize());
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get(homeToken);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject client = (JSONObject) map.get("client");
        Integer bid = client.getInteger("bid");
        Shop shop = shopMapper.findShopId(bid);
        Integer sid = shop.getId();
        System.out.println("====================="+sid);
        Page<Goods> page = new Page(goodsVo.getCurrentPage(),goodsVo.getPageSize());
        QueryWrapper<Goods> queryWrapper = new QueryWrapper();
        if(StringUtils.hasText(goodsVo.getGoodsname())){
            queryWrapper.like("g.gname",goodsVo.getGoodsname());
        }
        if(StringUtils.hasText(goodsVo.getBeginTime())){
            queryWrapper.ge("newgoodsdate",goodsVo.getBeginTime());
        }
        if(StringUtils.hasText(goodsVo.getEndTime())){
            queryWrapper.le("newgoodsdate",goodsVo.getEndTime());
        }
        queryWrapper.eq("gs.sid",sid);
        queryWrapper.eq("gs.is_deleted",0);
        queryWrapper.ne("gs.count",0);
        IPage<Map> iPage =  shopMapper.selectShopGoodsMsg(page,queryWrapper);
        return new CommonResult(2000,"查询成功",iPage);
    }

    @Override
    public CommonResult updateGoodsState(Integer state, Integer id) {
        if(state==0){
           Integer row =  shopMapper.updateGoodsState(id);
            return new CommonResult(2000,"下架成功",null);
        }else {
            Integer row = shopMapper.updateGoodsState02(id);
            return new CommonResult(2000,"上架成功",null);
        }
    }

    @Override
    public CommonResult addGoodsToShop(String gid, GoodsShopVo goodsShopVo, String homeToken) {
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get(homeToken);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject client = (JSONObject) map.get("client");
        Integer bid = client.getInteger("bid");
        Shop shop = shopMapper.findShopId(bid);
        Integer sid = shop.getId();
        //根据商品id和店铺id查看店铺中是否存在该商品
        GoodsShop goodsShop =  shopMapper.findGoodsByShop(gid,sid);
        if(goodsShop==null){
            //将商品添加到该表中
            goodsShopVo.setGid(gid);
            goodsShopVo.setSid(sid);
            Integer row =   shopMapper.addGoodsToShop(goodsShopVo);
            return new CommonResult(2000,"入店成功",null);
        }else {
            //修改价格叠加库存
            goodsShopVo.setGid(gid);
            goodsShopVo.setSid(sid);
            Integer count = goodsShop.getCount();
            goodsShopVo.setCount(goodsShopVo.getCount()+count);
            Integer row = shopMapper.updateGoodsShop(goodsShopVo);
            return new CommonResult(2000,"入店成功",null);
        }
    }

    @Override
    public CommonResult findByShopId(Integer id) {
        QueryWrapper<Shop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Shop shop = shopMapper.selectOne(queryWrapper);
        return new CommonResult(2000,"查找成功",shop);
    }

    @Override
    public CommonResult findByshopIdAllGoods(Integer pageCurrent, Integer pageSize, Integer shopId, Map<String, Object> names) {
        QueryWrapper queryWrapper = new QueryWrapper();
        System.out.println(names);
        Object names1 = names.get("names");
        if (names1!=null&&names1!="") {
            queryWrapper.like("g.gname", names.get("names"));
        }
        queryWrapper.eq("g.is_deleted",0);
        queryWrapper.eq("gs.state",0);
        queryWrapper.eq("gs.sid",shopId);
        Page page = new Page(pageCurrent,pageSize);
        IPage page1 = shopMapper.shopAndGoods(page, queryWrapper);
        return new CommonResult(2000,"查询成功",page1);


    }

    @Override
    public CommonResult findByZidAll(Integer id) {

        Map map = shopMapper.findByZidAll(id);
        return new CommonResult(2000,"查询成功",map);
    }
}
